/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package unb.wikinarua.geopartitura.view;

import unb.wikinarua.geopartitura.util.Constants;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.TypedArray;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class SeekBarPreference extends Preference implements
		OnSeekBarChangeListener {

	public static int maximum = 10;
	public static int minimum = 1;
	public static int interval = 1;

	private int oldValue = 10;
	private String unity = "pontos";
	private TextView monitorBox;

	public SeekBarPreference(Context context) {
		super(context);
	}

	public SeekBarPreference(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		if (attrs.getAttributeValue(null, "unity") != null)
			unity = attrs.getAttributeValue(null, "unity");
		maximum = attrs.getAttributeIntValue(null, "max", maximum);
		minimum = attrs.getAttributeIntValue(null, "min", minimum);
	}

	public SeekBarPreference(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	@Override
	protected View onCreateView(ViewGroup parent) {
		SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(getContext());
		oldValue = pref.getInt("radius", 10);
		
		LinearLayout layout = new LinearLayout(getContext());
		layout.setOrientation(LinearLayout.HORIZONTAL);
		layout.setGravity(Gravity.CENTER_VERTICAL);
		layout.setPadding(20, 20, 20, 20);

		// Label
		TextView view = new TextView(getContext());
		view.setText(getTitle());
		view.setTextAppearance(getContext(),
				android.R.style.TextAppearance_Large);
		view.setGravity(Gravity.LEFT);

		// SeekBar
		LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.FILL_PARENT,
				LinearLayout.LayoutParams.WRAP_CONTENT);
		params2.weight = 1;
		params2.gravity = Gravity.RIGHT;
		params2.leftMargin = 20;
		params2.rightMargin = 20;

		SeekBar bar = new SeekBar(getContext());
		bar.setMax(maximum);
		bar.setProgress(validateValue( (int) this.oldValue ));
		bar.setLayoutParams(params2);
		bar.setOnSeekBarChangeListener(this);

		// SeekBar selected value
		this.monitorBox = new TextView(getContext());
		this.monitorBox.setTextAppearance(getContext(),
				android.R.style.TextAppearance_Small);
		this.monitorBox.setText((bar.getProgress()) + " " + unity);

		layout.addView(view);
		layout.addView(bar);
		layout.addView(this.monitorBox);
		layout.setId(android.R.id.widget_frame);

		return layout;
	}

	@Override
	protected Object onGetDefaultValue(TypedArray ta, int index) {
		int dValue = (int) ta.getInt(index, 10);

		return validateValue(dValue);
	}

	@Override
	protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
		int temp = restoreValue ? getPersistedInt(10) : (Integer) defaultValue;

		if (!restoreValue)
			persistInt(temp);

		this.oldValue = temp;
	}

	private int validateValue(int value) {
		if (value > maximum)
			value = maximum;
		else if (value % interval != 0)
			value = Math.round(((float) value) / interval) * interval;

		if (value <= 0)
			value = 1;
		
		return value;
	}

	private void updatePreference(int newValue) {
		newValue = validateValue(newValue);
		Constants.RADIUS = newValue;
		
		SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(getContext()).edit();
		editor.putInt("radius", newValue);
		editor.commit();
	}

	public void onProgressChanged(SeekBar seekBar, int progress,
			boolean fromUser) {
		progress = Math.round(((float) progress) / interval) * interval;

		if (!callChangeListener(progress)) {
			seekBar.setProgress((int) this.oldValue);
			return;
		}

		seekBar.setProgress(progress);
		this.oldValue = progress;
		this.monitorBox.setText(validateValue(progress) + " " + unity);
		updatePreference(progress);

		callChangeListener(progress);
	}

	public void onStartTrackingTouch(SeekBar arg0) {

	}

	public void onStopTrackingTouch(SeekBar seekBar) {
	}
}
